﻿using System;
using System.Collections.Generic;
using WEB.Core.Controls;
using Ext.Net;
using WEB.Core.Util;
using log4net;
using WEB.BLL.Services;
using WEB.BLL.Entities;
using WEB.BLL;

namespace WEB.APP.Modules.List
{
    public partial class ClientDetail : BasePage
    {
        public ClientDetail()
            : base(SCREENS.CLIENTDETAIL, false)
        {
        }

        protected override void Page_Load(object sender, EventArgs e)
        {
            AddListControlAdd(tbsAdd);
            base.Page_Load(sender, e);
        }

        protected override void NotPostBack()
        {
            base.NotPostBack();
            if (CurrentUser.RoleId == 2)
            {
                txtClientId.Text = ClientService.GetInstance().LoadByUsername(new ClientEntity() { Username = CurrentUser.UserName }).ClientId.ToString();
                LoadData();
            }
            else if (!string.IsNullOrEmpty(Request["id"]))
            {
                ResourceManager.RegisterOnReadyScript(string.Format("txtClientId.setValue({0}); MethodNamespace.LoadData();", Request["id"]));
            }
        }

        [DirectMethod]
        public void LoadData()
        {
            try
            {
                if (!string.IsNullOrEmpty(txtClientId.Text))
                {
                    var _ett = ClientService.GetInstance().LoadPrimaryKey(new ClientEntity() { ClientId = Convert.ToDecimal(txtClientId.Value) });
                    if (_ett == null)
                        return;

                    txtClientId.Text = _ett.ClientId.ToString();
                    txtClientType.Text = _ett.ClientType;
                    txtSurName.Text = _ett.SurName;
                    txtGivenName.Text = _ett.GivenName;
                    cboSex.Value = _ett.Sex;
                    txtEmail.Text = _ett.Email;
                    txtAddr1.Text = _ett.Addr1;
                    txtAddr2.Text = _ett.Addr2;
                    txtAddr3.Text = _ett.Addr3;
                    txtAddr4.Text = _ett.Addr4;
                    txtAddr5.Text = _ett.Addr5;

                    txtUserName.Text = _ett.Username;
                    txtConfirmPassword.Text = txtPassword.Text = hfPassword.Text = _ett.Password;
                }
                else
                {
                    NewData();
                }
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }

        protected void NewData()
        {
            hfPassword.Text = null;
            winDetails.Reset();
            txtClientId.Reset();
        }

        public void btnAdd_Click(object sender, DirectEventArgs e)
        {
            NewData();
        }

        public void btnSave_Click(object sender, DirectEventArgs e)
        {
            try
            {
                var _ett = new ClientEntity()
                {
                    ClientType = txtClientType.Text,
                    SurName = txtSurName.Text,
                    GivenName = txtGivenName.Text,
                    Sex = Convert.ToBoolean(cboSex.Value),
                    Email = txtEmail.Text,
                    Addr1 = txtAddr1.Text,
                    Addr2 = txtAddr2.Text,
                    Addr3 = txtAddr3.Text,
                    Addr4 = txtAddr4.Text,
                    Addr5 = txtAddr5.Text,
                    Username = txtUserName.Text,
                    Password = hfPassword.Text == txtPassword.Text ? hfPassword.Text : Encryption.StringToMD5Hash(txtPassword.Text)
                };

                if (string.IsNullOrEmpty(txtClientId.Text))
                {
                    ClientService.GetInstance().Insert(_ett);
                    txtClientId.Text = _ett.ClientId.ToString();
                }
                else
                {
                    _ett.ClientId = Convert.ToDecimal(txtClientId.Text);
                    ClientService.GetInstance().Update(_ett);
                }
                Notification("Save Completed !!!");
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }

        [DirectMethod]
        public void Delete()
        {
            try
            {
                if (!string.IsNullOrEmpty(txtClientId.Text))
                {
                    ClientService.GetInstance().Delete(new ClientEntity() { ClientId = Convert.ToDecimal(txtClientId.Text) });
                    Notification("Delete Completed !!!");
                    NewData();
                }
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
    }
}
